package com.joysuch.wwyt.common.repository;

import com.joysuch.wwyt.common.entity.CommonIndexNotify;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/5/29 16:11
 */
public interface CommonIndexNotifyDao extends JpaRepository<CommonIndexNotify, Long>, JpaSpecificationExecutor<CommonIndexNotify> {

    @Transactional
    CommonIndexNotify save(CommonIndexNotify commonIndexNotify);

    List<CommonIndexNotify> findByEduCertUserId(Long eduCertUserId);

    List<CommonIndexNotify> findByRemindDate(LocalDate remindDate);

    @Query(value = "select c from CommonIndexNotify c where c.remindDate <=?1 and c.queueOrNot = 0")
    List<CommonIndexNotify> findByRemindDateAndQueueOrNot(LocalDate remindDate);

    @Query(value = "select c from CommonIndexNotify c where c.remindedOrNot = ?1 and c.readFlag = ?2")
    List<CommonIndexNotify> findByRemindedOrNotAndReadFlag(Integer remindOrNot, Integer readFlag);

    CommonIndexNotify findFirstByEduCertUserIdAndUserIdAndType(Long eduCertUserId, Long userId, String type);

    @Query(value = "select c from CommonIndexNotify c where c.userId =:userId and c.readFlag = 0 and c.remindedOrNot=1 and c.remindDateTime <= :date ")
    List<CommonIndexNotify> findByUserIdAndReadFlagAndRemindedOrNotAndDate(Long userId, LocalDateTime date);
 
    List<CommonIndexNotify> findByTypeAndEduCertUserId(String type, Long eduCertUserId);

    @Transactional
    @Modifying
    @Query(value = "update CommonIndexNotify c set c.deleteFlag = '1' where c.type= ?1 and c.recordId = ?2")
    void deleteByTypeAndRecordId(String type, Long recordId);

    List<CommonIndexNotify> findByTypeAndRecordId(String type, Long recordId);

    @Query(value = "select c from CommonIndexNotify c where c.remindDate <?1 and c.queueOrNot = 0 and c.remindedOrNot=0 ")
    List<CommonIndexNotify> findRemindList(LocalDate remindDate);

    @Query(value = "select * from wwyt_common_index_notify where user_id = ?3 and edu_cert_user_id in ?1 and read_flag = 0 and `type` = ?2 and delete_flag = '0'",nativeQuery = true)
    List<CommonIndexNotify> findByEduCertUserIdsAndType(List<Long> eduCertUserIds, String type, Long userId);
    
    @Query(value = "select count(*) from wwyt_common_index_notify where type = ?5 and remind_date = ?1 and business_date = ?2 and record_id = ?3  and user_id = ?4 and delete_flag = '0'",nativeQuery = true)
    Long needNotify(Date dateTime, Date businessDate, Long certUserId, Long remindUserId , String type);

    @Query(value = "select * from wwyt_common_index_notify where edu_cert_user_id in ?1 and type = ?2 and delete_flag = '0'",nativeQuery = true)
    List<CommonIndexNotify> findByTaskId(List<Long> taskByIds, String type);


    @Transactional
    @Modifying
    @Query(value = "update wwyt_common_index_notify c set c.delete_flag = '1' where c.edu_cert_user_id in ?1 and c.type = ?2 and c.queue_or_not = 0 " ,nativeQuery = true)
    void deleteByTaskIds(List<Long> taskIds , String type);

    @Transactional
    @Modifying
    @Query(value = "update wwyt_common_index_notify c set c.delete_flag = '1' where c.edu_cert_user_id in ?1 and c.type = ?2 and c.read_flag = 0 " ,nativeQuery = true)
    void deletePlanByTaskIds(List<Long> taskIds , String type);
    @Query(value = "select * from wwyt_common_index_notify where read_flag = 0 and delete_flag = '0'",nativeQuery = true)
    List<CommonIndexNotify> findCommonIndexNotifyNoRead();

}
